package com.lanou.zhihu.service.impl;

import com.lanou.zhihu.entity.AnswerGood;
import com.lanou.zhihu.entity.User;
import com.lanou.zhihu.mapper.AnswerGoodMapper;
import com.lanou.zhihu.service.AnswerGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by QuChao on 2018/10/31.
 */
@Service
public class AnswerGoodServiceImpl implements AnswerGoodService {
    @Autowired
    private AnswerGoodMapper answerGoodMapper;

    @Override
    public boolean findByUidAid(User user, Integer aid) {
        Map<String, Object> map = new HashMap<>();
        map.put("uid", user.getId());
        map.put("aid", aid);
        AnswerGood answerGood = answerGoodMapper.findByUidAid(map);
        if (answerGood != null) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean good(User user, Integer aid) {
        Map<String, Object> map = new HashMap<>();
        map.put("uid", user.getId());
        map.put("aid", aid);
        Integer result = answerGoodMapper.insert(map);
        if (result == 1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean cancelGood(User user, Integer aid) {
        Map<String, Object> map = new HashMap<>();
        map.put("uid", user.getId());
        map.put("aid", aid);
        Integer result = answerGoodMapper.deleteByUidAid(map);
        if (result == 1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Integer selectCount(Integer aid) {
        Integer result = answerGoodMapper.selectCount(aid);
        return result;
    }
}
